﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace GitUtil
{
    public class PomParseUtil
    {
        public ModuleInfo parsePom(String rootPath, String moduleName)
        {
            //判断当前路径下是否有pom文件
            String modulePath = rootPath + Path.DirectorySeparatorChar;

            DirectoryInfo dir = new DirectoryInfo(modulePath);
            Console.WriteLine("配置路径：{0}", dir.FullName);
            FileInfo[] files = dir.GetFiles("pom.xml");
            if (files == null || files.Length <= 0)
            {
                Console.WriteLine("当前路径未查询到POM文件，下级文件查找");
                return null;
            }
            else
            {
                Console.WriteLine("当前路径查询到POM文件，解析模块依赖");
            }

            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(modulePath + "pom.xml", settings);
            xmlDoc.Load(reader);

            XmlElement rootElement = xmlDoc.DocumentElement;//获取根节点
            if (rootElement == null)//没有根节点，或者说没有节点
            {
                Console.WriteLine("pom文件未获取到根节点，模块{0}的pom文件解析异常", moduleName);
                return null;
            }

            if (rootElement.HasChildNodes)//根节点有子节点
            {
                ModuleInfo parentModule = new ModuleInfo(); // 父模块信息
                parentModule.path = modulePath;

                if (moduleName != null) {
                    parentModule.name = moduleName;
                    ModuleInfo.AddModuleName(moduleName);
                }
                else {
                    parentModule.name = "parent";
                }
                
                XmlNode packaging = rootElement.SelectSingleNode("packaging");
                if (packaging != null)
                {
                    if ("war".Equals(packaging.InnerText.Trim().ToLower()))
                    {
                        parentModule.outType = OutType.WAR;
                    }
                    else if ("jar".Equals(packaging.InnerText.Trim().ToLower()))
                    {
                        parentModule.outType = OutType.WAR;
                    }
                    else
                    {
                        parentModule.outType = OutType.JAR;
                    }
                }
                else {
                    parentModule.outType = OutType.JAR;
                }

                XmlNodeList modules = rootElement.GetElementsByTagName("modules");
                //XmlNode modules = rootElement.SelectSingleNode("modules");
                if (modules!=null && modules.Count==1)
                {
                    Console.WriteLine("解析到 子模块");
                    foreach (XmlNode module in modules[0])
                    {
                        String childModuleName = module.InnerText;
                        Console.WriteLine(childModuleName);

                        parentModule.AddChidMoudle(parsePom(modulePath + childModuleName + Path.DirectorySeparatorChar, childModuleName));
                    }
                }

                //XmlNodeList childNodesList = rootElement.ChildNodes;
                //foreach (XmlElement childNode in childNodesList)
                //{
                //    if (childNode.Name.Equals("modules"))
                //    {
                //        Console.WriteLine("解析到 modules");
                //        foreach (XmlNode module in childNode.ChildNodes)
                //        {
                //            Console.WriteLine(module.InnerText);
                //        }
                //    }
                //}

                reader.Close();

                return parentModule;
            }
            return null;
        }
    }
}
